layui.use(['element', 'jquery', 'layer', 'laytpl','laypage','form','table'], function() {
    var $ = layui.jquery,
        layer = layui.layer,
        laytpl = layui.laytpl,
        laypage=layui.laypage,
        form=layui.form,
        element = layui.element
    table = layui.table;

    $(document).ready(function () {
        table.render({
            elem: '#after_tb'
            ,url:'/aftersale/findbyuserid'
            ,cols: [[
                {field:'aftersaleId', title:'售后编号', width:110, fixed: 'left',unresize: true,  sort: true}
                ,{field:'orderId', title:'订单编号', width:90}
                ,{field:'price', title:'支付金额', width:200,}
                ,{field:'pstatus', title:'支付状态',width:90,templet: function(res){
                        console.log(res.orderId);
                        data = res.pstatus;

                        if(res.pstatus!=0){
                            $("#p1").html("已支付");
                            return '<span class="layui-badge layui-bg-blue">已支付</span>';
                        }else
                        return '<span class="layui-badge">未支付</span>';
                    }}
                ,{field:'pstatus', title:'售后状态',width:90,templet: function(res){
                        console.log(res.pstatus);
                        data = res.pstatus;

                        if(res.pstatus!=0){
                            $("#p1").html("已支付");
                            switch (res.pstatus) {
                                case 1:  return '<span class="layui-badge layui-bg-blue">未发货</span>';
                                case 2:  return '<span class="layui-badge layui-bg-blue">已发货</span>';
                                case 3:  return '<span class="layui-badge layui-bg-blue">退货中</span>';
                                case 4:  return '<span class="layui-badge layui-bg-blue">已退货</span>';
                                case 5:  return '<span class="layui-badge layui-bg-blue">换货中</span>';
                                case 6:  return '<span class="layui-badge layui-bg-blue">已换货</span>';
                                case 7:  return '<span class="layui-badge layui-bg-blue">退货失败</span>';
                                case 8:  return '<span class="layui-badge layui-bg-blue">换货失败</span>';
                            }

                        }else
                        return '<span class="layui-badge"></span>';
                    }}
                ,{field: 'select1',title: '选择商品（默认全部）',width: 200,templet: function (res) {
                    var  oid =[] ;
                    var i=0;
                    $.ajax({
                        url:'/aftersale/list?orderId='+res.orderId,
                        async: false,
                        success:function (rgs) {
                            console.log(rgs.data.orderItems);
                            for(;i<rgs.data.orderItems.length;i++){
                               // console.log(rgs.data.orderItems[0].book_info.bookName);
                                oid[i] ='<option id="'+res.orderId+'"  value="'+[res.aftersaleId,rgs.data.orderItems[i].book_info.bookName]+'">'+ rgs.data.orderItems[i].book_info.bookName+'</option>'+"\n";
                            }
                        }
                    });
                        console.log(oid);
                        //把数组转换成字符串，数组名.join("分隔符")
                        str =oid.join("");
                        console.log(str);//style="margin-top: 10px;margin-left: 15px;margin-right: 15px"
                        return  '<select name="select1" lay-filter="testSelect" lay-verify="required"   >\n'+
                            '<option id="'+res.orderId+'"  value="'+[res.aftersaleId,"全部商品"]+'">'+"全部商品"+'</option>\n'+
                            str
                            +'</select>\n';
                    }}
                ,{fixed: 'right', title:'操作', toolbar: '#after_tb_bar', width:350}


    ]],done: function (res, curr, count) {
                var tableElem = this.elem.next('.layui-table-view');

                //count || tableElem.find('.layui-table-header').css('overflow', 'auto');
                layui.each(tableElem.find('select[name="select1"]'), function (index, item) {
                    var elem = $(item);
                    elem.val(elem.data('select1')).parents('div.layui-table-cell').css('overflow', 'visible');
                });
                form.render();//刷新表单
            }
            ,page: 2
            ,limit:10
            ,limits:[5,15,30,60,100]
        });
    });

    layui.form.on('select(testSelect)', function (data) {
        var data1 = data.value.split(',');
        $.ajax({
            url:'/aftersale/updatejiajuname?aftersaleId='+data1[0]+'&&jiajuName='+data1[1],
            success:function (res) {
                return layer.msg("选择商品为："+data1[1], {icon: 1});
            },
            error:function () {
                return layer.msg("服务器错误,请稍后再试", {icon: 2});
            }
        })
        //console.log(data1[0],data1[1]);
    })
    /* 设置下拉框与表格单元相同 */
   /* td .layui-form-select {
        margin-top: -10px;
        margin-left: -15px;
        margin-right: -15px;
    }*/


    //监听行工具事件
    table.on('tool(after_tb)', function(obj){
        var data = obj.data;
        console.log(data);
         if(obj.event === 'del'){
             layer.confirm('真的删除该记录么', function(index){
                 $.ajax({
                     url:'/aftersale/deleteafter/'+data.aftersaleId,
                    /* type:'delete',
                     dataType:'json',*/
                     success:function (res) {
                         if(res.code!=0){
                             return layer.msg(res.msg, {icon: 2});
                         }
                         return layer.msg("删除成功", {icon: 1},function () {
                             obj.del();
                             $("#order-items").html("");
                         });
                     },
                     error:function () {
                         return layer.msg("服务器错误,请稍后再试", {icon: 2});
                     }
                 });
                 layer.close(index);
             });
         } else if(obj.event === 'return'){
            console.log(data.pstatus);
            //0:未付款，1:已付款，待发货，2：已发货，3：退货中，4：已退货，5：换货中，6：已换货
             //退货的状态不应该有除了2以外的状态
            if(data.pstatus!=2){
                switch (data.pstatus) {
                    case 1:return layer.msg("未发货，请等待！", {icon: 2});
                    case 3:return layer.msg("退货中", {icon: 2});
                    case 4:return layer.msg("已退货", {icon: 2});
                    case 5:return layer.msg("换货中", {icon: 2});
                    case 6:return layer.msg("已退货", {icon: 2});
            }
            }else (data.pstatus == 2)
                // console.log("111111");
                tz();
            function tz() {
                $.ajax({
                    url: '/aftersale/updateafter?orderId='+data.orderId+'&pstatus='+3,
                    success:function (res) {
                        //console.log(res);
                        return layer.msg("退货申请成功，请等待商家同意", {icon: 1});
                    },
                    error:function () {
                        return layer.msg("服务器错误,请稍后再试", {icon: 2});
                    }
                })

            }

        }else if(obj.event === 'barter'){
             console.log(data.pstatus);
             //0:未付款，1:已付款，待发货，2：已发货，3：退货中，4：已退货，5：换货中，6：已换货
             //退货的状态不应该有除了2以外的状态
             if(data.pstatus!=2){
                 switch (data.pstatus) {
                     case 1:return layer.msg("未发货，请等待！", {icon: 2});
                     case 3:return layer.msg("退货中", {icon: 2});
                     case 4:return layer.msg("已退货", {icon: 2});
                     case 5:return layer.msg("换货中", {icon: 2});
                     case 6:return layer.msg("已退货", {icon: 2});
                 }
             }else (data.pstatus == 2)
                 // console.log("111111");
                 tz();
             function tz() {
                 $.ajax({
                     url: '/aftersale/updateafter?orderId='+data.orderId+'&pstatus='+5,
                     success:function (res) {
                         //console.log(res);
                         return layer.msg("换货申请成功，请等待商家同意", {icon: 1});
                     },
                     error:function () {
                         return layer.msg("服务器错误,请稍后再试", {icon: 2});
                     }
                 })

             }

         }else if(obj.event === 'call'){
             layer.open({
                 title: '联系商家',
                 content: '联系电话为12345678',
                 btn:'确定',
                 yes:function (index1) {
                     layer.close(index1);
                 }
             })
         }
    });


});
